import io
import re
import time

import bs4
import requests
from PIL import Image
from selenium import webdriver
from selenium.webdriver import ActionChains

from chaojiying import ChaojiyingClient

driver = webdriver.Chrome()
driver.set_window_size(1200, 960)
driver.get('https://passport.bilibili.com/login')
driver.implicitly_wait(10)
tab2 = driver.find_element_by_css_selector('#geetest-wrap > div > div.type-tab > span:last-child')
tab2.click()
tel_input = driver.find_element_by_name('tel')
tel_input.send_keys('16713494705')
get_code_button = driver.find_element_by_css_selector('#geetest-wrap > div > div:nth-child(3) > div:nth-child(3) > button')
get_code_button.click()
# wait = WebDriverWait(driver, 10)
# wait.until(expected_conditions.presence_of_element_located(
#     (By.CSS_SELECTOR, 'body > div.geetest_panel.geetest_wind > div.geetest_panel_box.geetest_no_logo.geetest_panelshowclick > div.geetest_panel_next > div > div')
# ))
time.sleep(3)
captcha = driver.find_element_by_css_selector('body > div.geetest_panel.geetest_wind > div.geetest_panel_box.geetest_no_logo.geetest_panelshowclick > div.geetest_panel_next > div > div')
screen_image_data = driver.get_screenshot_as_png()
# 通过WebElement对象的location和size属性获取元素的位置和宽高
location, size = captcha.location, captcha.size
print(location, size)
width, height = size['width'], size['height'] - 46
left, top, right, bottom = location['x'], location['y'], location['x'] + width, location['y'] + height
image = Image.open(io.BytesIO(screen_image_data))
# image.save('screenshot.png')
# 通过Pillow库的Image对象的crop方法实现对图片的裁剪（Windows不用乘以2）
captcha_image = image.crop((left * 2, top * 2, right * 2, bottom * 2))
# 生成缩略图（Windows不需要这一步）
captcha_image.thumbnail((width, height))
# 将剪裁的验证码图片写到内存中并上传给超级鹰识别
buffer = io.BytesIO()
captcha_image.save(buffer, format='png')
# captcha_image.show()
client = ChaojiyingClient('jackfrued', '1Qaz2Wsx', '900260')
result = client.post_pic(buffer.getvalue(), '9004')
print(result)
if result['err_no'] == 0:
    # 通过ActionChain模拟鼠标点击
    ac = ActionChains(driver)
    # 可以使用下面的方法来拖拽元素
    ac.drag_and_drop_by_offset(captcha, 300, 0)
    pic_str = result['pic_str']
    for item in pic_str.split('|'):
        x, y = map(int, item.split(','))
        # 将鼠标移动到元素上并按照指定的偏移量找到点击位置
        ac.move_to_element_with_offset(captcha, x, y).click()
    time.sleep(0.5)
    ac.perform()
confirm_button = driver.find_element_by_css_selector('.geetest_commit_tip')
confirm_button.click()
time.sleep(10)
pattern = re.compile(r'\d{6}')
resp = requests.get('https://www.yinsiduanxin.com/china-phone-number/verification-code-16713494705.html')
soup = bs4.BeautifulSoup(resp.text, 'html.parser')
# print(resp.text)
td = soup.select_one('body > div.container > div:nth-child(4) > div:nth-child(3) > div.main > div.layui-row > table > tbody > tr:nth-child(1) > td:nth-child(2)')
results = pattern.findall(td.text)
code = results[0] if len(results) > 0 else '000000'
code_input = driver.find_element_by_css_selector('input[placeholder="请输入短信验证码"]')
code_input.send_keys(code)
login_button = driver.find_element_by_css_selector('.btn-login')
login_button.click()
